package zhishuang.wang.sansi.fcms.devinfor;

/**
 * @Author Zhishuang.Wang
 * @Date 2023/7/5 10:51
 * @Email 1406110602@qq.com
 */


import zhishuang.wang.sansi.fcms.protocol.FcmsDeviceControl;
import zhishuang.wang.sansi.playlist.fcms.PlayListFcms;
import zhishuang.wang.sansi.tools.ReturnData;
import zhishuang.wang.sansi.tools.time.DateFormat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeviceControl {
    public DeviceControl() {
    }

    public static ReturnData fcmsVersionRead(String deviceId) {
        return fcmsDeviceCtrl(deviceId, 23, (Map)null);
    }

    private static ReturnData fcmsStartSelfTest(String deviceId) {
        return fcmsDeviceCtrl(deviceId, 21, (Map)null);
    }

    public static ReturnData fcmsFaultQurey(String deviceId) {
        return fcmsDeviceCtrl(deviceId, 1, (Map)null);
    }

    private static ReturnData fcmsNowShowQurey(String deviceId) {
        return fcmsDeviceCtrl(deviceId, 2, (Map)null);
    }

    public static ReturnData fcmsLightAndModeQurey(String deviceId) {
        return fcmsDeviceCtrl(deviceId, 3, (Map)null);
    }

    public static ReturnData fcmsLightAndModeSet(String deviceId, int lightMode, int lightVal) {
        Map<String, String> map = new HashMap();
        map.put("lightMode", String.valueOf(lightMode));
        map.put("lightVal", String.valueOf(lightVal));
        return fcmsDeviceCtrl(deviceId, 4, map);
    }

    private static ReturnData fcmsBlockSizeSet(String deviceId, int blockSize) {
        Map<String, String> map = new HashMap();
        map.put("paramType", "1");
        map.put("paramVal", String.valueOf(blockSize));
        return fcmsDeviceCtrl(deviceId, 5, map);
    }

    private static ReturnData fcmsBlockSizeRead(String deviceId) {
        Map<String, String> map = new HashMap();
        map.put("paramType", "1");
        return fcmsDeviceCtrl(deviceId, 6, map);
    }

    private static ReturnData fcmsResolutionRead(String deviceId) {
        Map<String, String> map = new HashMap();
        map.put("paramType", "2");
        return fcmsDeviceCtrl(deviceId, 22, map);
    }

    public static ReturnData fcmsTimeSet(String deviceId) {
        return fcmsDeviceCtrl(deviceId, 7, (Map)null);
    }

    public static ReturnData fcmsTimeRead(String deviceId) {
        return fcmsDeviceCtrl(deviceId, 8, (Map)null);
    }

    public static ReturnData fcmsNowPictureQuery(String deviceId, String localFileName) {
        if (localFileName != null && localFileName.endsWith(".bmp")) {
            Map<String, String> map = new HashMap();
            map.put("localFileName", localFileName);
            return fcmsDeviceCtrl(deviceId, 9, map);
        } else {
            ReturnData rd = new ReturnData();
            rd.setCode(1);
            rd.setMessage("存储文件格式必须是：bmp格式");
            rd.setTime(DateFormat.getNowDataFormat());
            return rd;
        }
    }

    private static ReturnData fcmsLedFaulteQuery(String deviceId, String localFileName) {
        if (localFileName.endsWith(".map")) {
            Map<String, String> map = new HashMap();
            map.put("localFileName", localFileName);
            return fcmsDeviceCtrl(deviceId, 10, map);
        } else {
            ReturnData rd = new ReturnData();
            rd.setCode(1);
            rd.setMessage("存储文件格式必须是：map格式");
            rd.setTime(DateFormat.getNowDataFormat());
            return rd;
        }
    }

    private static String calDevivePath(String devFileName) {
        String[] nameArray = devFileName.split("\\.");
        String filePath = "";
        if (nameArray.length == 2) {
            String suffixName = nameArray[1];
            if (suffixName.equals("bmp")) {
                filePath = "./bmp/" + devFileName;
            } else if (suffixName.equals("jpg")) {
                filePath = "./jpg/" + devFileName;
            } else if (suffixName.equals("gif")) {
                filePath = "./gif/" + devFileName;
            } else if (suffixName.equals("mpg")) {
                filePath = "./mpg/" + devFileName;
            } else if (suffixName.equals("mp4")) {
                filePath = "./mp4/" + devFileName;
            } else if (suffixName.equals("png")) {
                filePath = "./png/" + devFileName;
            } else if (suffixName.equals("rds")) {
                filePath = ".rds/" + devFileName;
            } else if (devFileName.equals("play.lst")) {
                filePath = "./" + devFileName;
            }
        } else {
            filePath = "./playlist/" + devFileName;
        }

        return filePath;
    }

    public static ReturnData fcmsDownFile(String deviceId, String localFileName, String devFileName) {
        String fileFath = calDevivePath(devFileName);
        if (fileFath.length() > 0) {
            Map<String, String> map = new HashMap();
            map.put("localFileName", localFileName);
            map.put("devFileName", fileFath);
            return fcmsDeviceCtrl(deviceId, 11, map);
        } else {
            ReturnData rd = new ReturnData();
            rd.setCode(1);
            rd.setMessage("设备上存储的文件名非法");
            rd.setTime(DateFormat.getNowDataFormat());
            return rd;
        }
    }

    public static ReturnData fcmsUploadFile(String deviceId, String localFileName, String devFileName) {
        String fileFath = calDevivePath(devFileName);
        System.out.println(fileFath);
        if (fileFath.length() > 0) {
            Map<String, String> map = new HashMap();
            map.put("localFileName", localFileName);
            map.put("devFileName", fileFath);
            return fcmsDeviceCtrl(deviceId, 12, map);
        } else {
            ReturnData rd = new ReturnData();
            rd.setCode(1);
            rd.setMessage("设备上存储的文件名非法");
            rd.setTime(DateFormat.getNowDataFormat());
            return rd;
        }
    }

    public static ReturnData fcmsActivePlayList(String deviceId, String playListName) {
        Map<String, String> map = new HashMap();
        map.put("playListName", playListName);
        return fcmsDeviceCtrl(deviceId, 13, map);
    }

    private static ReturnData fcmsStopPlayList(String deviceId, String playListName) {
        Map<String, String> map = new HashMap();
        map.put("devFileName", playListName);
        return fcmsDeviceCtrl(deviceId, 14, map);
    }

    private static ReturnData fcmsDeletePlayList(String deviceId, String playListName) {
        Map<String, String> map = new HashMap();
        if (playListName.equals("play.lst")) {
            playListName = "./" + playListName;
        } else {
            playListName = "./playlist/" + playListName;
        }

        map.put("devFileName", playListName);
        return fcmsDeviceCtrl(deviceId, 15, map);
    }

    public static ReturnData fcmsDeleteFile(String deviceId, String devFileName) {
        Map<String, String> map = new HashMap();
        map.put("devFileName", devFileName);
        return fcmsDeviceCtrl(deviceId, 16, map);
    }

    public static ReturnData fcmsUploadPlayList(String deviceId, String playListFile) {
        Map<String, String> map = new HashMap();
        map.put("playListFile", playListFile);
        return fcmsDeviceCtrl(deviceId, 17, map);
    }

    public static ReturnData fcmsFileInfor(String deviceId) {
        String[] dirArray = new String[]{"./", "./bmp", "./gif", "./jpg", "./mp4", "./mpg", "./playlist", "./png", "./rds", "./font"};
        ReturnData resRd = null;

        String devFileName = "";
        List<String> fileInfoList = new ArrayList();
        String localFileName = System.currentTimeMillis() + ".txt";
        File tFile = new File(localFileName);
        Map<String, String> map = new HashMap();
        map.put("localFileName", localFileName);

        for(int i = 0; i < dirArray.length; ++i) {
            map.put("fileName", dirArray[i]);
            resRd = fcmsDeviceCtrl(deviceId, 18, map);
            if (resRd.getCode() != 0) {
                break;
            }

            FileInputStream inputStream = null;
            BufferedReader bufferedReader = null;
            String lineStr = "";

            try {
                inputStream = new FileInputStream(localFileName);
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                for(lineStr = bufferedReader.readLine(); lineStr != null; lineStr = bufferedReader.readLine()) {
                    String[] lineArray = lineStr.split(" ");
                    int lineArrayLen = lineArray.length;
                    if (lineArrayLen > 3) {
                        --lineArrayLen;
                        devFileName = lineArray[lineArrayLen];
                        if (i == 0) {
                            if (devFileName.equals("play.lst")) {
                                devFileName = "./play.lst";
                                fileInfoList.add(devFileName);
                            }
                        } else if (!devFileName.equals(".") && !devFileName.equals("..")) {
                            devFileName = dirArray[i] + "/" + devFileName;
                            fileInfoList.add(devFileName);
                        }
                    }
                }
            } catch (FileNotFoundException var15) {
                var15.printStackTrace();
            } catch (IOException var16) {
                var16.printStackTrace();
            }

            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }

                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException var14) {
                var14.printStackTrace();
            }
        }

        if (tFile.exists()) {
            tFile.delete();
        }

        if (resRd != null) {
            if (resRd.getCode() == 0) {
                resRd.setData(fileInfoList);
                resRd.setMessage("获取设备文件信息成功");
            }
        } else {
            resRd = new ReturnData();
            resRd.setCode(1);
            resRd.setMessage("获取设备文件信息异常");
        }

        resRd.setTime(DateFormat.getNowDataFormat());
        return resRd;
    }

    public static ReturnData fcmsReboot(String deviceId) {
        return fcmsDeviceCtrl(deviceId, 19, (Map)null);
    }

    private static ReturnData fcmsSwitchCtrl(String deviceId, int switchVal) {
        Map<String, String> map = new HashMap();
        map.put("switchVal", String.valueOf(switchVal));
        return fcmsDeviceCtrl(deviceId, 20, map);
    }

    private static ReturnData uploadPlayListFiles(DeviceInfor di, Map<String, String> paramMap, FcmsDeviceControl fcmsDeviceControl) {
        String playListFile = (String)paramMap.get("playListFile");
        ReturnData rd = new ReturnData();
        ReturnData resRd = null;
        rd.setCode(1);
        if (playListFile != null && playListFile.length() != 0) {
            File pFile = new File(playListFile);
            if (!pFile.exists()) {
                rd.setMessage("播放表文件不存在");
                rd.setTime(DateFormat.getNowDataFormat());
                return rd;
            } else {
                String fileName = pFile.getName();
                String fileDir = pFile.getParent();
                FileInputStream inputStream = null;
                BufferedReader bufferedReader = null;
                String lineStr = "";

                try {
                    inputStream = new FileInputStream(playListFile);
                    bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "GBK"));

                    for(lineStr = bufferedReader.readLine(); lineStr != null && !lineStr.startsWith(";{"); lineStr = bufferedReader.readLine()) {
                    }
                } catch (FileNotFoundException var27) {
                    var27.printStackTrace();
                } catch (IOException var28) {
                    var28.printStackTrace();
                } finally {
                    try {
                        if (bufferedReader != null) {
                            bufferedReader.close();
                        }

                        if (inputStream != null) {
                            inputStream.close();
                        }
                    } catch (IOException var25) {
                        var25.printStackTrace();
                    }

                }

                if (lineStr != null && lineStr.length() > 3) {
                    lineStr = lineStr.substring(1);
                    resRd = PlayListFcms.uploadCheckFileExist(lineStr, fileDir);
                    if (resRd.getCode() != 0) {
                        return resRd;
                    }

                    List<String> mfileList = (List)resRd.getData();
                    int tSize = mfileList.size();
                    if (tSize % 2 != 0) {
                        resRd.setCode(1);
                        resRd.setMessage("播放表文件验证错误");
                        return resRd;
                    }

                    for(int i = 0; i < mfileList.size(); i += 2) {
                        Map<String, String> mFileUpMap = new HashMap();
                        mFileUpMap.put("localFileName", mfileList.get(i));
                        mFileUpMap.put("devFileName", mfileList.get(i + 1));
                        resRd = fcmsDeviceControl.fcmsFileUpload(di, mFileUpMap);
                        if (resRd.getCode() != 0) {
                            String errStr = "播放表素材上传错误：" + (String)mfileList.get(i) + "：" + resRd.getMessage();
                            resRd.setMessage(errStr);
                            return resRd;
                        }
                    }
                }

                Map<String, String> playUpMap = new HashMap();
                playUpMap.put("localFileName", playListFile);
                String errStr;
                if (fileName.equals("play.lst")) {
                    playUpMap.put("devFileName", "play.lst");
                } else {
                    if (fileName.length() != 3) {
                        resRd.setCode(1);
                        resRd.setMessage("播放表名称规则：play.lst或由三个可见字符组成，可见字符范围：a-z,A-Z,0-9");
                        return resRd;
                    }

                    errStr = "playlist/" + fileName;
                    playUpMap.put("devFileName", errStr);
                }

                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException var26) {
                    var26.printStackTrace();
                }

                resRd = fcmsDeviceControl.fcmsFileUpload(di, playUpMap);
                if (resRd.getCode() == 0) {
                    resRd.setMessage("上传播放表成功");
                } else {
                    errStr = "播放表文件上传错误：" + resRd.getMessage();
                    resRd.setMessage(errStr);
                }

                return resRd;
            }
        } else {
            rd.setMessage("播放表文件不可为空");
            rd.setTime(DateFormat.getNowDataFormat());
            return rd;
        }
    }

    private static ReturnData fcmsDeviceCtrl(String deviceId, int cmdFlag, Map<String, String> paramMap) {
        ReturnData rd = new ReturnData();
        ReturnData resRd = null;
        rd.setCode(1);
        DeviceVar.deviceInforMapLock.lock();
        DeviceInfor di = (DeviceInfor) DeviceVar.deviceInforMap.get(deviceId);
        if (di != null) {
            if (di.getControFlag() == 0) {
                di.setControFlag(1);
                DeviceVar.deviceInforMap.put(deviceId, di);
                DeviceVar.deviceInforMapLock.unlock();
                FcmsDeviceControl fcmsDeviceControl = null;

                try {
                    fcmsDeviceControl = new FcmsDeviceControl(di.getIpAddr(), di.getIpPort(), 5000);
                    label53:
                    switch (di.getFcmsVersion()) {
                        case 1:
                            switch (cmdFlag) {
                                case 1:
                                    resRd = fcmsDeviceControl.fcmsFaultQuery(di);
                                    break label53;
                                case 2:
                                    resRd = fcmsDeviceControl.fcmsShowInforQuery(di);
                                    break label53;
                                case 3:
                                    resRd = fcmsDeviceControl.fcmsReadLighValAndMode(di);
                                    break label53;
                                case 4:
                                    resRd = fcmsDeviceControl.fcmsSetLightAndMode(di, paramMap);
                                    break label53;
                                case 5:
                                    resRd = fcmsDeviceControl.fcmsParamSetDeal(di, paramMap);
                                    break label53;
                                case 6:
                                case 22:
                                    resRd = fcmsDeviceControl.fcmsParamReadDeal(di, paramMap);
                                    break label53;
                                case 7:
                                    resRd = fcmsDeviceControl.FcmsTimeSetDeal(di);
                                    break label53;
                                case 8:
                                    resRd = fcmsDeviceControl.FcmsTimeReadDeal(di);
                                    break label53;
                                case 9:
                                    paramMap.put("devFileName", "currentframe.bmp");
                                    resRd = fcmsDeviceControl.fcmsFileDown(di, paramMap);
                                    break label53;
                                case 10:
                                    resRd = fcmsDeviceControl.fcmsLedSelfTest(di);
                                    if (resRd.getCode() == 0) {
                                        Thread.sleep(15000L);
                                        paramMap.put("devFileName", "badpix.map");
                                        resRd = fcmsDeviceControl.fcmsFileDown(di, paramMap);
                                    }
                                    break label53;
                                case 11:
                                    resRd = fcmsDeviceControl.fcmsFileDown(di, paramMap);
                                    break label53;
                                case 12:
                                    resRd = fcmsDeviceControl.fcmsFileUpload(di, paramMap);
                                    break label53;
                                case 13:
                                    resRd = fcmsDeviceControl.fcmsSetPreset(di, paramMap);
                                    break label53;
                                case 14:
                                    rd.setMessage("协议版本不支持，目前只支持：fcmsVersion 为 1");
                                    break label53;
                                case 15:
                                case 16:
                                    resRd = fcmsDeviceControl.fcmsDeleteFile(di, paramMap);
                                    break label53;
                                case 17:
                                    resRd = uploadPlayListFiles(di, paramMap, fcmsDeviceControl);
                                    break label53;
                                case 18:
                                    String tFileName = (String)paramMap.get("fileName");
                                    resRd = fcmsDeviceControl.fcmsFileListQuery(di, tFileName);
                                    if (resRd.getCode() == 0) {
                                        paramMap.put("devFileName", "dir.txt");
                                        resRd = fcmsDeviceControl.fcmsFileDown(di, paramMap);
                                    }
                                    break label53;
                                case 19:
                                    resRd = fcmsDeviceControl.fcmsReboot(di);
                                    break label53;
                                case 20:
                                    resRd = fcmsDeviceControl.fcmsDeviceSwitch(di, paramMap);
                                    break label53;
                                case 21:
                                    resRd = fcmsDeviceControl.fcmsLedSelfTest(di);
                                    break label53;
                                case 23:
                                    resRd = fcmsDeviceControl.fcmsDevVersionRead(di);
                                    break label53;
                                default:
                                    rd.setMessage("FCMS设备指令不支持");
                                    break label53;
                            }
                        default:
                            rd.setMessage("协议版本不支持，目前只支持：fcmsVersion 为 1");
                    }
                } catch (Exception var8) {
                    var8.printStackTrace();
                    rd.setMessage("设备操作异常");
                }

                if (fcmsDeviceControl != null) {
                    fcmsDeviceControl.closeSocketConenct();
                }

                DeviceVar.deviceInforMapLock.lock();
                di.setControFlag(0);
                DeviceVar.deviceInforMap.put(deviceId, di);
                DeviceVar.deviceInforMapLock.unlock();
            } else {
                DeviceVar.deviceInforMapLock.unlock();
                rd.setMessage("设备信息正在控制中，请稍后");
            }
        } else {
            DeviceVar.deviceInforMapLock.unlock();
            rd.setMessage("设备信息未初始化");
        }

        if (resRd != null) {
            return resRd;
        } else {
            rd.setTime(DateFormat.getNowDataFormat());
            return rd;
        }
    }
}

